package com.qiwx.pigeon.fragment;

import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import com.google.android.material.tabs.TabLayout;
import com.qiwx.pigeon.R;
import com.qiwx.pigeon.activity.MainActivity;
import com.qiwx.pigeon.adapter.query.TabPagerAdapter;
import com.qiwx.pigeon.base.BaseFragment;
import com.qiwx.pigeon.data.vo.WareHouseBean;
import com.qiwx.pigeon.fragment.produce.DataChartFragment;
import com.qiwx.pigeon.fragment.query.QueryCageFragment;
import com.qiwx.pigeon.fragment.query.QueryPigeoFactoryFragment;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * @description
 * @Author qiwx
 * @Date 2020-03-23 14:00
 * 查询
 **/
public class QueryFragment extends BaseFragment {
    @BindView(R.id.tab_layout)
    TabLayout tabLayout;
    @BindView(R.id.viewPager)
    ViewPager viewPager;
    private List<Fragment> fragmentList = new ArrayList<>();
    TabPagerAdapter adapter;
    String[] titles;
    WhListenerble whListenerble;

    @Override
    public int getLayoutId() {
        return R.layout.fragment_query;
    }

    @Override
    public void initAfterOnCreate() {
        //titles = new String[]{"鸽厂生产情况查询", "鸽仓生产情况查询", "鸽笼生产详情"};
//        titles = new String[]{"鸽厂查询", "鸽仓查询", "鸽笼详情"};
        titles = new String[]{"统计表", "鸽笼详情"};
        initWhListener();
        initFragments();
        initView();

    }

    public void setOnWhLister(WhListenerble whLister) {

    }

    private void initFragments() {
        fragmentList.clear();
//        QueryPigeoFactoryFragment factory = new QueryPigeoFactoryFragment();
//        QueryPigeoFactoryFragment warehouse = new QueryPigeoFactoryFragment();
        DataChartFragment dataChartFragment = new DataChartFragment();
        QueryCageFragment cageFragment = new QueryCageFragment();
//        Bundle bundle = new Bundle();
//        bundle.putInt("type", 1);
//        dataChartFragment.setArguments(bundle);
//        Bundle bundle1 = new Bundle();
//        bundle1.putInt("type", 0);
//        warehouse.setArguments(bundle1);
//        fragmentList.add(factory);
//        fragmentList.add(warehouse);
        fragmentList.add(dataChartFragment);
        fragmentList.add(cageFragment);
    }

    @Override
    public void onResume() {
        super.onResume();
        //TODO 获取db数据库存放目录
//        LogUtil.e("TAG", LitePal.getDatabase().getPath());

    }

    private void initView() {
        adapter = new TabPagerAdapter(getChildFragmentManager(), titles, fragmentList);
        viewPager.setAdapter(adapter);
        viewPager.setOffscreenPageLimit(3);
        tabLayout.setupWithViewPager(viewPager);
        tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                //选中某个tab
                onChangeData(viewPager.getCurrentItem());

            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                //当tab从选择到未选择
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {
                //已经选中tab后的重复点击tab
            }
        });
    }

    public void onReFresh(int pos) {
        //鸽场不需要查询 只查鸽仓
        if (pos == 0) {
//            ((QueryPigeoFactoryFragment) fragmentList.get(pos)).loadData();
            ((DataChartFragment) fragmentList.get(pos)).changeData();

        } else if (pos == 1) {
            ((QueryCageFragment) fragmentList.get(pos)).loadData(true);

        }
    }

    public void onChangeData(int pos) {
        //如果鸽仓id不变不用更新
        if (pos == 0) {
            ((DataChartFragment) fragmentList.get(pos)).changeData();
        } else if (pos == 1) {
            ((QueryCageFragment) fragmentList.get(pos)).changeData();

        }
    }

    //监听鸽仓
    public void initWhListener() {
        if (getActivity() instanceof MainActivity) {
            ((MainActivity) getActivity()).setWareHouseListener(new MainActivity.WareHouseListener() {
                @Override
                public void onWhSuccess(List<WareHouseBean> list) {

                }

                @Override
                public void onSelectWareHouse(WareHouseBean wareHouseBean) {
                    if (whListenerble != null) {
                        whListenerble.onWhChangeListener(wareHouseBean);
                    }
                    onReFresh(viewPager.getCurrentItem());

                }
            });
        }
    }

    interface WhListenerble {
        void onWhChangeListener(WareHouseBean wareHouseBean);
    }
}
